package com.fc.commons;

import static java.util.Calendar.DATE;
import static java.util.Calendar.DAY_OF_WEEK;
import static java.util.Calendar.HOUR_OF_DAY;
import static java.util.Calendar.MILLISECOND;
import static java.util.Calendar.MINUTE;
import static java.util.Calendar.SECOND;
import static java.util.Calendar.WEEK_OF_YEAR;
import static java.util.Calendar.YEAR;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;

/**
 * 日期工具类，用于简化程序中的日期处理
 */
public class DateUtil extends DateUtils {

	/** 日期时间格式:年-月-日 时:分:秒[2011-5-5 20:00:00] */
	public static final String PATTERN_DATE_TIME = "yyyy-MM-dd HH:mm:ss";
	/** 日期格式:年-月-日[2011-05-05] */
	public static final String PATTERN_DATE = "yyyy-MM-dd";
	/** 时间格式:时:分:秒[20:00:00] */
	public static final String PATTERN_TIME = "HH:mm:ss";
	/** 短时间格式:时:分[20:00] */
	public static final String PATTERN_SHORT_TIME = "HH:mm";

	/**
	 * 检查当前时间和指定时间是否同一周
	 * 
	 * @param year
	 *            年
	 * @param week
	 *            周
	 * @param firstDayOfWeek
	 *            周的第一天设置值，{@link Calendar#DAY_OF_WEEK}
	 * @return
	 */
	public static boolean isSameWeek(int year, int week, int firstDayOfWeek) {
		Calendar cal = Calendar.getInstance();
		cal.setFirstDayOfWeek(firstDayOfWeek);
		return year == cal.get(YEAR) && week == cal.get(WEEK_OF_YEAR);
	}

	/**
	 * 检查当前时间和指定时间是否同一周
	 * 
	 * @param time
	 *            被检查的时间
	 * @param firstDayOfWeek
	 *            周的第一天设置值，{@link Calendar#DAY_OF_WEEK}
	 * @return {@link Boolean} 是否同一周. true-是, false-不是
	 */
	public static boolean isSameWeek(Date time, int firstDayOfWeek) {
		if (time == null) {
			return false;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		cal.setFirstDayOfWeek(firstDayOfWeek);
		return isSameWeek(cal.get(YEAR), cal.get(WEEK_OF_YEAR), firstDayOfWeek);
	}

	/**
	 * 获取周的第一天
	 * 
	 * @param firstDayOfWeek
	 *            周的第一天设置值，{@link Calendar#DAY_OF_WEEK}
	 * @param time
	 *            指定时间，为 null 代表当前时间
	 * @return {@link Date} 周的第一天
	 */
	public static Date firstTimeOfWeek(int firstDayOfWeek, Date time) {
		Calendar cal = Calendar.getInstance();
		if (time != null) {
			cal.setTime(time);
		}

		cal.setFirstDayOfWeek(firstDayOfWeek);
		int day = cal.get(DAY_OF_WEEK);
		if (day == firstDayOfWeek) {
			day = 0;
		} else if (day < firstDayOfWeek) {
			day = day + (7 - firstDayOfWeek);
		} else if (day > firstDayOfWeek) {
			day = day - firstDayOfWeek;
		}

		cal.set(HOUR_OF_DAY, 0);
		cal.set(MINUTE, 0);
		cal.set(SECOND, 0);
		cal.set(MILLISECOND, 0);

		cal.add(DATE, -day);
		return cal.getTime();
	}

	/**
	 * 检查指定日期是否今天(使用系统时间)
	 * 
	 * @param date
	 *            被检查的日期
	 * @return {@link Boolean} 是否今天, true-今天, false-不是今天
	 */
	public static boolean isToday(Date date) {
		if (date == null) {
			return false;
		}
		return isSameDay(date, new Date());
	}
	
	/**
	 * 格式化时间为一个字符串
	 * @param date	被格式化的时间
	 * @param pattern	格式
	 * @return	{@link String} 格式化的字符串
	 */
	public static String formatDate(Date date,String pattern){
		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		return dateFormat.format(date);
	}
}
